<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="退单号" prop="refundNo">
        <el-input v-model="queryParams.refundNo" placeholder="请输入退单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单号 " prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号 " clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商户单订单号" prop="merchantOrderNo" label-width="100px">
        <el-input v-model="queryParams.merchantOrderNo" placeholder="请输入商户单订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="退单类型" prop="type">
      <el-select v-model="queryParams.type" placeholder="请选择退单类型" clearable size="small">
        <el-option v-for="dict in statusDictDatas"
                   :key="dict.value" :label="dict.label" :value="dict.value"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_pay_status_refund)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="退单状态" prop="refundStatus">
        <el-select v-model="queryParams.refundStatus" placeholder="请选择退单状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.pc_refund_merchant_status)"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="扣款时间" prop="deductTime">
        <el-date-picker v-model="queryParams.deductTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>

      <el-form-item label="退货发货时间" prop="returnDeliveryTime" label-width="100px">
        <el-date-picker v-model="queryParams.returnDeliveryTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['pekingfish:sale-order-refund:create']">新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['pekingfish:sale-order-refund:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" border  :max-height="tableHeight" :data="list">
      <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width">
        <template v-slot="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['pekingfish:sale-order-refund:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['pekingfish:sale-order-refund:delete']">删除</el-button>-->
          <el-button v-if="scope.row.refundStatus === 1" size="mini" type="text" icon="el-icon-edit"
                     @click="handleRefundGoods(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:sale-order-refund:return']"
          >发货
          </el-button>
          <el-button v-if="scope.row.refundStatus === 0" size="mini" type="text" icon="el-icon-edit"
                     @click="handleConfirmRefund(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:sale-order-refund:confirmRefund']"
          >确认退款
          </el-button>
          <el-button size="mini" type="text" @click="handleView(scope.row,scope.index)"
                     v-hasPermi="['pekingfish:sale-order-refund:detail']"
          >订单详细
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退单号" align="center" prop="refundNo" />
      <el-table-column label="订单号 " align="center" prop="orderNo" />
      <el-table-column label="商户单订单号" align="center" prop="merchantOrderNo"  width="150" fixed="left"/>
      <el-table-column label="退单类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.sale_return_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="退单状态" align="center" prop="refundStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_refund_merchant_status" :value="scope.row.refundStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.pc_pay_status_refund" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="手机品牌" align="center" prop="brand" />
      <el-table-column label="手机型号" align="center" prop="model" />
      <el-table-column label="型号类别" align="center" prop="modelType" />
      <el-table-column label="回收价" align="center" prop="paybackPrice" />
      <el-table-column label="销售回收价" align="center" prop="salePaybackPrice"  width="150"/>
      <el-table-column label="扣款金额" align="center" prop="deductionPrice" />
      <el-table-column label="订单时间" align="center" prop="orderTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="扣款时间" align="center" prop="deductTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.deductTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货发货时间" align="center" prop="returnDeliveryTime" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.returnDeliveryTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货物流单号" align="center" prop="logisticsNo" width="150"/>

    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退单号" prop="refundNo">
          <el-input v-model="form.refundNo" placeholder="请输入退单号" />
        </el-form-item>
        <el-form-item label="订单号 " prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号 " />
        </el-form-item>
        <el-form-item label="商户单订单号" prop="merchantOrderNo">
          <el-input v-model="form.merchantOrderNo" placeholder="请输入商户单订单号" />
        </el-form-item>
        <el-form-item label="手机品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入手机品牌" />
        </el-form-item>
        <el-form-item label="手机品牌" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入手机品牌" />
        </el-form-item>
        <el-form-item label="手机型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入手机型号" />
        </el-form-item>
        <el-form-item label="型号类别" prop="modelType">
          <el-select v-model="form.modelType" placeholder="请选择型号类别">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="回收价" prop="paybackPrice">
          <el-input v-model="form.paybackPrice" placeholder="请输入回收价" />
        </el-form-item>
        <el-form-item label="销售回收价" prop="salePaybackPrice">
          <el-input v-model="form.salePaybackPrice" placeholder="请输入销售回收价" />
        </el-form-item>
        <el-form-item label="退单状态" prop="refundStatus">
          <el-radio-group v-model="form.refundStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="支付状态" prop="payStatus">
          <el-radio-group v-model="form.payStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="扣款金额" prop="deductionPrice">
          <el-input v-model="form.deductionPrice" placeholder="请输入扣款金额" />
        </el-form-item>
        <el-form-item label="订单时间" prop="orderTime">
          <el-date-picker clearable v-model="form.orderTime" type="date" value-format="timestamp" placeholder="选择订单时间" />
        </el-form-item>
        <el-form-item label="扣款时间" prop="deductTime">
          <el-date-picker clearable v-model="form.deductTime" type="date" value-format="timestamp" placeholder="选择扣款时间" />
        </el-form-item>
        <el-form-item label="退单类型 deductMoney 扣款, returnGoods 退货" prop="type">
          <el-select v-model="form.type" placeholder="请选择退单类型 deductMoney 扣款, returnGoods 退货">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="退货发货时间" prop="returnDeliveryTime">
          <el-date-picker clearable v-model="form.returnDeliveryTime" type="date" value-format="timestamp" placeholder="选择退货发货时间" />
        </el-form-item>
        <el-form-item label="退货物流单号" prop="logisticsNo">
          <el-input v-model="form.logisticsNo" placeholder="请输入退货物流单号" />
        </el-form-item>
        <el-form-item label="验机仓id" prop="checkWarehouseId">
          <el-input v-model="form.checkWarehouseId" placeholder="请输入验机仓id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 订单详情 -->
    <el-dialog title="订单详情" :visible.sync="openDetail" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="30">
            <el-form-item label="品牌：">{{ form.brand }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="手机型号：">{{ form.model }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="型号类型：">{{ form.modelType }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="IMEI：">{{ form.imei }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数：">{{ form.memory }}，{{ form.colour }}，{{ form.buy }}，{{
                form.network
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="机况：">{{ form.subrackDesc }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片：">
              <imageUpload v-model="form.reportImg"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDetail = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { createSaleOrderRefund, updateSaleOrderRefund, deleteSaleOrderRefund, getSaleOrderRefund,
  saleOrderRetunPage, refundGoods,
  getSaleOrderRefundPage, exportSaleOrderRefundExcel } from "@/api/pekingfish/saleOrderRefund";

import {
  confirmRefund
} from "@/api/pekingfish/appMerchantOrderRefund";

import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import ImageUpload from '@/components/ImageUpload'
import ImagePreview from '@/components/ImagePreview'
import VideoUpload from "@/components/VideoUpload";
import Editor from "@/components/Editor";
export default {
  name: "SaleOrderRefund",
  components: {
    VideoUpload, Editor,
    ImageUpload, ImagePreview,
  },
  data() {
    return {
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      statusDictDatas: getDictDatas(DICT_TYPE.pc_shop_type),
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单退单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        createTime: [],
        refundNo: null,
        orderNo: null,
        merchantOrderNo: null,
        brand: null,
        brandId: null,
        model: null,
        modelType: null,
        paybackPrice: null,
        salePaybackPrice: null,
        refundStatus: null,
        payStatus: null,
        deductionPrice: null,
        orderTime: [],
        deductTime: [],
        type: null,
        returnDeliveryTime: [],
        logisticsNo: null,
        checkWarehouseId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantOrderNo: [{ required: true, message: "商户单订单号不能为空", trigger: "blur" }],
      },
      openDetail: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      saleOrderRetunPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        refundNo: undefined,
        orderNo: undefined,
        merchantOrderNo: undefined,
        brand: undefined,
        brandId: undefined,
        model: undefined,
        modelType: undefined,
        paybackPrice: undefined,
        salePaybackPrice: undefined,
        refundStatus: undefined,
        payStatus: undefined,
        deductionPrice: undefined,
        orderTime: undefined,
        deductTime: undefined,
        type: undefined,
        returnDeliveryTime: undefined,
        logisticsNo: undefined,
        checkWarehouseId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售订单退单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getSaleOrderRefund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售订单退单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSaleOrderRefund(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSaleOrderRefund(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除销售订单退单编号为"' + id + '"的数据项?').then(function() {
          return deleteSaleOrderRefund(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有销售订单退单数据项?').then(() => {
          this.exportLoading = true;
          return exportSaleOrderRefundExcel(params);
        }).then(response => {
          this.$download.excel(response, '销售订单退单.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openDetail = true
      this.form = row
    },

    /** 发货按钮操作 */
    handleRefundGoods(row) {
      console.log(row.shopRefundNo)
      this.$prompt('请输入"' + row.refundNo + '"的物流单号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({value}) => {
        refundGoods(row.id, value).then(response => {
          this.getList()
          this.$modal.msgSuccess('发货成功')
        })
      }).catch(() => {
      })
    },

    handleConfirmRefund(row) {
      const merchantOrderNo = row.merchantOrderNo
      const refundPrice = row.deductionPrice
      this.$modal.confirm('是否确认同意商户单订单号为"' + merchantOrderNo + '"的数据退款?').then(function () {
        return confirmRefund(row.id, row.merchantOrderNo,refundPrice)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('退款成功')
      }).catch(() => {
      })
    },

  }
};
</script>
